import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { TechFileInfoDto, TechFileInfoServiceProxy, OrganizationUnitServiceProxy, ComboboxItemDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'dispenseTechFileInfo',
  templateUrl: './dispense-techfileinfo.component.html'
})

export class DispenseTechFileInfoComponent extends AppComponentBase {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('tmplCombobox') tmplCombobox: ElementRef;
  @ViewChild('stageTypeCombobox') stageTypeCombobox: ElementRef;
  @ViewChild('securityTypeCombobox') securityTypeCombobox: ElementRef;
  @ViewChild('techFileInfoTypeCombobox') techFileInfoTypeCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  order: TechFileInfoDto = new TechFileInfoDto();
  companyCombobox: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
  stages: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
  securitys: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
  filetypes: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
  realCompanyCodesStr;

  constructor(
    injector: Injector,
    private _techFileInfoService: TechFileInfoServiceProxy,
    private _organizationUnitServiceProxy: OrganizationUnitServiceProxy
  ) {
    super(injector);
  }

  show(id?: string): void {
    this.active = true;
    this.getstage();
    this.getsecurity();
    this.getfiletype();
    this.getcompanycode();
    this._techFileInfoService.getTechFileInfoForEdit(id).subscribe(result => {
      this.order = result;
      this.realCompanyCodesStr=result.realCompanyCodesStr?result.realCompanyCodesStr:this.order.companyCodesStr;
      setTimeout(() => {
        $(this.stageTypeCombobox.nativeElement).selectpicker('val', this.order.stageType);
        $(this.securityTypeCombobox.nativeElement).selectpicker('val', this.order.securityType);
        $(this.techFileInfoTypeCombobox.nativeElement).selectpicker('val', this.order.techFileInfoType);
        $(this.stageTypeCombobox.nativeElement).selectpicker('refresh');
        $(this.securityTypeCombobox.nativeElement).selectpicker('refresh');
        $(this.techFileInfoTypeCombobox.nativeElement).selectpicker('refresh');
      }, 1);

      this.modal.show();
    });
  }

  getcompanycode() {
    this.companyCombobox = [];
    this._organizationUnitServiceProxy
      .getAllCompany()
      .subscribe((result) => {
        result.forEach((c) => {
          let companyItem = new ComboboxItemDto();
          companyItem.value = c.code;
          companyItem.displayText = c.displayName;
          this.companyCombobox.push(companyItem);
        });
      });
  }

  getstage() {
    this._techFileInfoService.getComboboxItemForStageType().subscribe(result => {
      this.stages = result;
    });
  }

  getsecurity() {
    this._techFileInfoService.getComboboxItemForSecurityType().subscribe(result => {
      this.securitys = result;
    });
  }

  getfiletype() {
    this._techFileInfoService.getComboboxItemForTechFileInfoType().subscribe(result => {
      this.filetypes = result;
    });
  }

  save(): void {
    this.saving = true;
    this._techFileInfoService.distributeTechFiles(this.order.id, this.realCompanyCodesStr)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
      });
  }

  addCode(code:string) {
    if (code) {
      let codename = this.companyCombobox.find(c => c.value == code).displayText;
      if (this.realCompanyCodesStr != "")
        this.realCompanyCodesStr += "-";
      this.realCompanyCodesStr += codename + "-";

      //去除尾部“-”
      if (this.realCompanyCodesStr.length > 1)
        this.realCompanyCodesStr = this.realCompanyCodesStr.substr(0, this.realCompanyCodesStr.length - 1);
    }
  }

  delCode(code:string) {
    if (this.realCompanyCodesStr && code) {
      let codename = this.companyCombobox.find(c => c.value == code).displayText;
      let tempstr = "";
      let i = 0;
      if (this.realCompanyCodesStr.indexOf("-") < 0 && this.realCompanyCodesStr == codename)
        this.realCompanyCodesStr = "";
      else {
        this.realCompanyCodesStr.split("-").forEach(element => {
          if (codename == element && i == 0)
            i++;
          else
            tempstr += element + "-";
        });
        //去除尾部“-”
        if (tempstr.length > 1)
          this.realCompanyCodesStr = tempstr.substr(0, tempstr.length - 1);
      }
    }
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
